#include "data_mange_check.h"
#include "ui_data_mange_check.h"

data_mange_check::data_mange_check(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::data_mange_check)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_QuitOnClose,false);
    sql = QSqlDatabase::addDatabase("QSQLITE", "eigth");
    sql.setDatabaseName("child.db");
    bool open_or_not = sql.open();
    if(open_or_not == true){
        qDebug() << "数据库连接成功";
    }
    else{
        qDebug() << "数据库连接失败";
    }
    Init();
    connect(ui->pushButton_print, SIGNAL(clicked(bool)), this, SLOT(click_print()));
}


void data_mange_check::display_date_check(int id){

    QSqlQuery query(sql);
    QString sql_text = "SELECT * FROM child_data LEFT JOIN tb_result ON child_data.id=tb_result.child_id WHERE child_data.id=" + QString::number(id) + " ORDER BY child_data.id DESC LIMIT 1;";
    qDebug() <<sql_text;

    query.exec(sql_text);
    query.next();

    ui->label_ID->setText(query.value(12).toString());
    ui->label_code->setText(query.value(0).toString());
    ui->label_birth->setText(query.value(3).toString());


    ui->label_com->setText(query.value(14).toString());
    //    ui->label_com->setText("儿童医院");

    ui->label_date->setText(query.value(13).toString());

    ui->label_family->setText(family_list[query.value(6).toInt()]);
    ui->label_f_e->setText(edu_list[query.value(7).toInt()]);
    ui->label_f_w->setText(work_list[query.value(9).toInt()]);
    ui->label_m_e->setText(edu_list[query.value(8).toInt()]);
    ui->label_m_w->setText(work_list[query.value(10).toInt()]);
    ui->label_grade->setText(grade_list[query.value(4).toInt()]);

    ui->label_name->setText(query.value(1).toString());
    ui->label_phone->setText(query.value(5).toString());

    if(query.value(20).toInt() < 40){
        ui->label_res->setText("未通过");
    }
    else{
        ui->label_res->setText("通过");
    }

    ui->label_sex->setText(sex_list[query.value(2).toInt()]);
    ui->label_score1->setText(query.value(15).toString());
    ui->label_score2->setText(query.value(16).toString());
    ui->label_score3->setText(query.value(17).toString());
    ui->label_score4->setText(query.value(18).toString());
    ui->label_score5->setText(query.value(19).toString());


    ui->textEdit->setText(text);

    this->show();
}

void data_mange_check::closeEvent(QCloseEvent *event){
    qDebug() << "关了诶";
    emit child_close();
}

void data_mange_check::Init(){
    sex_list << "女" << "男";
    grade_list << "一年级" << "二年级" << "三年级" << "四年级" << "五年级"\
               << "六年级" << "七年级" << "八年级" << "九年级";
    family_list << "核心家庭" << "主干家庭" << "联合家庭" << "单亲家庭" << "重组家庭" << "丁克家庭";

    work_list.append(tr("国家机关党群组织"));
    work_list.append(tr("专业技术人员"));
    work_list.append(tr("办事人员和有关人员"));
    work_list.append(tr("商业服务业人员"));
    work_list.append(tr("农林牧渔水利"));
    work_list.append(tr("生产运输"));
    work_list.append(tr("军人"));
    work_list.append(tr("不便分类"));
    work_list.append(tr("无职业"));

    edu_list.append(tr("博士"));
    edu_list.append(tr("硕士"));
    edu_list.append(tr("本科"));
    edu_list.append(tr("大专"));
    edu_list.append(tr("中专与中技"));
    edu_list.append(tr("高中"));
    edu_list.append(tr("初中"));
    edu_list.append(tr("小学"));
    edu_list.append(tr("文盲与半文盲"));

    text += "  不要刻意在别人面前掩饰自己的羞怯、紧张，社交过程中，把注意力多放在对方和交谈内容上面，不要过分关注自己的表现。";
    text += "  家长不过分关注孩子症状，不要紧盯孩子表现，对其焦虑症状不批评不责备，给孩子良好的家庭支持，促进孩子心身健康发展。";
    text += "  做好孩子的学校管理，创造有利于儿童的适应过程与环境，减轻儿童压力，增强自信。";
    text += "  做好日常生活管理，培养孩子良好的性格和行为习惯：如保证充足的睡眠、建立积极的生活兴趣。" ;
    text += "  多进行规律的有氧运动，可以每天进行户外活动。";
    text += "  适当的放松训练，多让孩子参加文娱、体育活动，缓解孩子的心理恐惧。";

}

void data_mange_check::click_print(){
    // 配置打印机
    QPrinter printer;
    printer.setPageSize(QPrinter::A4);
    printer.setPrinterName("数据表");
    printer.setOutputFileName("test.pdf");

    QPrintPreviewDialog preview(&printer, this);
    connect(&preview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(printPreview(QPrinter*)));
    preview.exec();
    qDebug() << "打印";

}

void data_mange_check::printPreview(QPrinter *pPrinter)
{
    QString strHtml = "</head><body><h1>儿童档案</h1><h2>儿童基本信息</h2><p>档案ID:</p>";
    strHtml += "<p>" + ui->label_ID->text() + "</p>";
    strHtml += "<p>儿童姓名 ";
    strHtml += "    " + ui->label_name->text() + "  ";
    strHtml += "    性别： ";
    strHtml += "    " + ui->label_sex->text() + "</p>";
    strHtml += "<P>儿童编号：";
    strHtml += "    " + ui->label_code->text() + "  ";
    strHtml += "    电话号码：</P>";
    strHtml += "<p>" + ui->label_phone->text() + "  ";
    strHtml += "    年级： ";
    strHtml += "    " + ui->label_grade->text() + "</p>";
    strHtml += "<P>出生日期：    ";
    strHtml += "    " + ui->label_birth->text() + "</p>";
    strHtml += "<h2>家庭情况：</h2>";
    strHtml += "<P>家庭类型：";
    strHtml += "    " + ui->label_family->text() + "</p>";
    strHtml += "<P>父亲文化程度：</P>";
    strHtml += "<p>" + ui->label_f_e->text() + "</p>";
    strHtml += "<P>母亲文化程度：</P>";
    strHtml += "<p>" + ui->label_m_e->text() + "</p>";
    strHtml += "<P>父亲职业：</P>";
    strHtml += "<p>" + ui->label_f_w->text() + "</p>";
    strHtml += "<P>母亲职业：</P>";
    strHtml += "<p>" + ui->label_m_w->text() + "</p>";
    strHtml += "<h2>筛查情况：</h2>";
    strHtml += "<P>测试结果：</P>";
    strHtml += "<p>" + ui->label_res->text() + "</p>";
    strHtml += "<P>测试单位：</P>";
    strHtml += "<p>" + ui->label_com->text() + "</p>";
    strHtml += "<P>结论及建议：</P>";
    strHtml += "<p>" + ui->textEdit->toPlainText() + "</p>";
    strHtml += "</body></html>";

    QTextDocument doc;
    doc.setHtml(strHtml);
    doc.print(pPrinter);
}

void data_mange_check::display_print(int id){
        display_date_check(id);
}

data_mange_check::~data_mange_check()
{
    delete ui;
}
